package org.roger.index;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.roger.util.FileUtil;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class LuceneIndex {
	
	private Directory dir;
	
	private Analyzer defaultAnalyzer;
	
	/**
	 * 构造器
	 * @param analyzer	默认解析器
	 * @param indexPath	索引路径
	 */
	public LuceneIndex(Analyzer analyzer,String indexPath){
		try {
			defaultAnalyzer = analyzer;
			dir = FSDirectory.open(new File(indexPath));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void index(){
		try {
			createIndex();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void createIndex() throws IOException{
		IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LATEST,defaultAnalyzer);
		
		IndexWriter indexWriter = new IndexWriter(dir, iwConfig);
		indexWriter.addDocument(createDucument(1,new File("E:/lucene/file/1.txt"),"20,7"));
		indexWriter.addDocument(createDucument(2,new File("E:/lucene/file/2.txt"),"30,12"));
		indexWriter.addDocument(createDucument(3,new File("E:/lucene/file/3.txt"),"22,18"));
		indexWriter.commit();
		indexWriter.close();
	}
	
	private Document createDucument(int id,File file,String localtion){
		return createDucument(id,file.getName(),FileUtil.getText(file.getPath()),file.lastModified(),localtion);
	}
	
	private Document createDucument(int id,String name,String content,Long lastTime,String location){
		Document doc = new Document();
		doc.add(new IntField("id",id,Field.Store.YES));
		doc.add(new StringField("name",name,Field.Store.YES));
		doc.add(new LongField("lastTime",lastTime,Field.Store.YES));
		doc.add(new StringField("location",location,Field.Store.YES));
		
		FieldType confieldType = new FieldType(TextField.TYPE_NOT_STORED);
		confieldType.setIndexed(true);
		confieldType.setStored(true);
		doc.add(new Field("content",content,confieldType));
		
		return doc;
	}

	/**
	 * 
	 * @param writer
	 * @param field
	 * @param keyword
	 * @throws IOException
	 */
	public static void updateDoc(String indexPath, int keyword)
			throws IOException {
		Directory dir = FSDirectory.open(new File(indexPath));
		Analyzer analyzer = new StandardAnalyzer();
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LATEST, analyzer);
		// MergePolicy

		IndexWriter writer = new IndexWriter(dir, iwc);

		Document doc = new Document();

		doc.add(new StringField("id", "4", Field.Store.YES));

		doc.add(new StringField("path", "path/wwwwwwwww", Field.Store.YES));

		doc.add(new LongField("modified", 102123123l, Field.Store.YES));

		doc.add(new TextField("contents", "last search back why!!!",
				Field.Store.YES));

		writer.updateDocument(new Term("contents", "why"),
				(Iterable<? extends IndexableField>) doc);

		writer.close();
	}
	
	

	public static void main(String[] args) {
		
//		List<String> stopWords = new ArrayList<String>();
//		stopWords.add("the");
//		CharArraySet stopWordsSet = StopFilter.makeStopSet(stopWords);
//		LuceneIndex.index(new SplitSpotAnalyzer(stopWordsSet), "D:/file", "D:/index2");
		
		//LuceneIndex.index(new StandardAnalyzer(), "D:/file", "D:/index");
	//	LuceneIndex.index(new MetaPhoneAnalyzer(), "D:/file", "D:/index1");

//		 SynonymEngineImpl se = new SynonymEngineImpl();
//		 se.setValue("your", new String[]{"you","your","yours"});
		Analyzer analyzer = new IKAnalyzer(true);
		LuceneIndex index = new LuceneIndex(analyzer,"E:/lucene/index");
		index.index();
	//	LuceneIndex.index(new SynonymAnalyzer(), "D:/lucene/file", "D:/lucene/index");

		// try {
		// LuceneIndex.updateDoc("D:/index", 4);
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

	}
}
